package com.dhnsoft.education.controller.edu;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dhnsoft.education.entity.edu.EduCourse;
import com.dhnsoft.education.entity.edu.EduVideo;
import com.dhnsoft.education.handle.EduException;
import com.dhnsoft.education.service.edu.EduVideoService;
import com.dhnsoft.education.utils.R;
import com.dhnsoft.education.utils.vod.ConstantVodUtils;
import com.dhnsoft.education.utils.vod.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author 代浩楠
 * @since 2022-02-19
 */
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    //删除小节
    @DeleteMapping("deleteVideo/{videoId}")
    public R deleteVideo(@PathVariable String videoId){
        //根据小节id获取视频id，调用方法实现视频删除
        EduVideo eduVideo = eduVideoService.getById(videoId);
        String videoSourceId = eduVideo.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){
            //根据视频id，远程调用实现视频删除
            R result = removeAlyVideo(videoSourceId);
            if(result.getCode() == 20001){
                throw new EduException(20001,"删除视频失败，熔断器");
            }
        }
        //删除小节
        eduVideoService.removeById(videoId);
        return R.ok();
    }

    //修改小节
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }

    //根据小节id查询
    @GetMapping("/getVideoById/{videoId}")
    public R getVideoById(@PathVariable String videoId){
        EduVideo eduVideo = eduVideoService.getById(videoId);
        return R.ok().data("video",eduVideo);
    }

    //根据章节id查询所有小节
    @GetMapping("/getVideoByChapterId/{chapterId}")
    public R getVideoByChapterId(@PathVariable String chapterId){
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        List<EduVideo> list = eduVideoService.list(wrapper);
        return R.ok().data("videoList",list);
    }

    public R removeAlyVideo(@PathVariable String id){
        try {
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESSKEY_ID, ConstantVodUtils.ACCESSKEY_SECRET);
            //创建一个删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(id);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            throw new EduException(20001,"删除视频失败");
        }
    }
    //增加视频浏览量
    @GetMapping("addViewCount/{id}")
    public R addViewCount(@PathVariable String id){
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("video_source_id",id);
        EduVideo video = eduVideoService.getOne(wrapper);
        video.setPlayCount(video.getPlayCount()+1);
        eduVideoService.updateById(video);
        return R.ok().data("video",video);
    }


}

